home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / prodpack.zip / DB4PPSRC.EXE / _STRTRAN.PRG < prev    next >
Text File  |  1993-05-04  |  3KB  |  106 lines

  1. FUNCTION _StrTran    && Performs string search and replace operations
  2. PARAMETERS pc_target, pc_locate, pc_replace, pn_first, pn_times
  3. *--------------------------------------------------------------------
  4. * NAME
  5. *   _STRTRAN - performs a search and replace on a
  6. *              character string.
  7. *
  8. * SYNOPSIS
  9. *   _STRTRAN( pc_target, pc_locate, pc_replace
  10. *             [, pn_first [, pn_times ] ] )
  11. *
  12. * DESCRIPTION
  13. *   _STRTRAN() allows a variety of search and replace
  14. *   operations to be performed on a string.  Unlike
  15. *   STUFF(), it can perform repeated replacements
  16. *   within the target string.
  17. *
  18. *   _STRTRAN() is similar to STUFF(), except that it
  19. *   also allows you to specify when to begin replacing
  20. *   the string, and how many times to replace it.
  21. *
  22. *   If no occurrences of the pc_locate string are
  23. *   found, the pc_target string will be returned
  24. *   unchanged.
  25. *
  26. *   Only the first three parameters are required.
  27. *   If pn_first and pn_times are not passed, _StrTran
  28. *   will start at the first occurrence and replace all.
  29. *   If pn_times is not passed, _StrTran will replace
  30. *   all by default.
  31. *
  32. * PARAMETERS
  33. *   pc_target   = character string to search in
  34. *   pc_locate   = character string to look for
  35. *   pc_replace  = replacement string
  36. *   pn_first    = number of first occurrence to replace
  37. *   pn_times    = number of occurrences to replace
  38. *
  39. * EXAMPLE
  40. *
  41. *   STORE "This_is_a_test" TO mvar
  42. *
  43. *   * Replace every occurrence of "_":
  44. *   ? _STRTRAN( mvar, "_", " ", 1, 3 )
  45. *   * Returns "This is a test"
  46. *
  47. *   * Replace every occurrence of "_":
  48. *   ? _STRTRAN( mvar, "_", " ", 1, 500 )
  49. *   * Returns "This is a test"
  50. *
  51. *   * Replace second occurrence of "_":
  52. *   ? _STRTRAN( mvar, "_", "X", 2, 1 )
  53. *   * Returns "This_isXa_test"
  54. *
  55. * SEE ALSO:
  56. *   AT(), STUFF()
  57. *
  58. *--------------------------------------------------------------------
  59.  
  60.   PRIVATE lc_target, ln_first, ln_foundct, ln_incr, ln_index, ln_loclen, ;
  61.           ln_patch, ln_replct, ln_replen, ln_times, nLenTarget
  62.  
  63.   ln_foundct = 0
  64.   ln_replct = 0
  65.   ln_patch = 0
  66.   ln_index = 1
  67.   lc_target = pc_target
  68.   nLenTarget = LEN( pc_target )
  69.   ln_first = IIF( PCOUNT() = 3,  1, m->pn_first )
  70.   ln_times = IIF( PCOUNT() < 5, 255, m->pn_times )
  71.   ln_loclen = LEN(m->pc_locate)
  72.   ln_replen = LEN(m->pc_replace)
  73.  
  74.   DO WHILE ln_index <= nLenTarget
  75.     ln_patch = AT(m->pc_locate, SUBSTR(m->lc_target, m->ln_index) )
  76.  
  77.     IF m->ln_patch = 0
  78.       EXIT
  79.     ELSE
  80.       ln_index = ( m->ln_index - 1 ) + m->ln_patch
  81.       ln_foundct = m->ln_foundct + 1
  82.  
  83.       IF m->ln_foundct >= m->ln_first
  84.  
  85.         IF m->ln_replct < m->ln_times
  86.           lc_target = STUFF(m->lc_target, m->ln_index, ;
  87.                             m->ln_loclen, m->pc_replace)
  88.  
  89.           ln_replct = m->ln_replct + 1
  90.           ln_incr = m->ln_replen
  91.         ELSE
  92.           EXIT
  93.         ENDIF
  94.  
  95.       ELSE
  96.         ln_incr = m->ln_loclen
  97.       ENDIF
  98.  
  99.       ln_index = ( m->ln_index + m->ln_incr )
  100.     ENDIF
  101.  
  102.   ENDDO
  103.  
  104. RETURN( m->lc_target )
  105. *-- EOF: _StrTran(  pc_target, pc_locate, pc_replace, pn_first, pn_times )
  106.